package com.ycd.controller;

import com.ycd.service.IMovieTicketService;
import com.ycd.domain.MovieTicket;
import com.ycd.result.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/movieTicket")
public class MovieTicketController {

    @Autowired
    public IMovieTicketService movieTicketService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody MovieTicket movieTicket){
        if(movieTicket.getId()!=null){
            movieTicketService.updateById(movieTicket);
        }else{
            movieTicketService.insert(movieTicket);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        movieTicketService.deleteById(id);
        return JsonResult.success();
    }



    /**
     * 查询所有对象-电影票查询：查询所有电影票信息并返回
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        // movieTicketService.selectList(null); 条件构造器传null，就是查所有
        return JsonResult.success(movieTicketService.selectList(null));
    }

    /**
     * 根据id买票同时，保存用户、电影票中间表 -涉及两张表
     */
    @GetMapping("/buyTicket/{userId}/{movieId}")
    public JsonResult buyTicket(@PathVariable("userId") Long userId,@PathVariable("movieId") Long movieId){
        return movieTicketService.buyTicket(userId,movieId);
    }

    /**
     * 根据电影Id，查询用户是否拥有此电影票
     */
    @GetMapping("/watchMovie/{userId}/{movieId}")
    public JsonResult watchMovie(@PathVariable("userId") Long userId,@PathVariable("movieId") Long movieId){
        return movieTicketService.watchMovie(userId,movieId);
    }

}
